package com.complex.spring.advance.service.context;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class TestPropertiesLoader {
	private String filePath;
	
	public Properties getProperties(){
		 Properties p = new Properties();
	        try {
	        	//InputStream in = PropertyUtil.class.getResourceAsStream(fileName); 这样也可以
	        	//将文件读取为InputStream  再从InputStream中解析成Properties
	        	//此处主要是为了得到ClassLoader,如何取得ClassLoader 方法较多
	            System.out.println(TestPropertiesLoader.class.getClassLoader());
	            System.out.println(getClass().getClassLoader());
	            //InputStream in = TestPropertiesLoader.class.getClassLoader().getResourceAsStream(filePath);
	        	//Object.class.getClassLoader().getResourceAsStream(filePath);
	            //使用classloader反而不行?
	            //filePath 写成 /bean/test.properties时 ClassLoader.getResourceAsStream(name)无法加载,原因是name有问题,去掉最前面的/就可以
	            //Class.getResourceAsStream(name)中有 name = resolveName(name); 所以可以加载到
	            InputStream in = getClass().getResourceAsStream(filePath);
	            p.load(in);
	            in.close();
	        } catch (Exception e1) {
	            try {
	                InputStream in = getClass().getResourceAsStream("/" + filePath);
	                p.load(in);
	                in.close();
	            } catch (Exception e2) {
	                try {
	                    File f = new File(filePath);
	                    FileInputStream fis = new FileInputStream(f);
	                    p.load(fis);
	                    fis.close();
	                } catch (Exception e3) {
	                    //TODO
	                }
	            }
	        }
	        return p;
	}
	
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	
	
	
	
}
